﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using W.Common.Enum;
using W.Purchase.Domain;
using W.Purchase.Domain.DTO;
using W.Purchase.Domain.PageDTO;
using W.Purchase.Read.API.AppLication.Command.PayableCommand;
using W.Purchase.Read.API.AppLication.Command.ReceivableCommand;

namespace W.Purchase.Read.API.Controllers
{
    /// <summary>
    /// 应付管理控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PayableController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<PayableController> _logger;
        public PayableController(IMediator mediator, ILogger<PayableController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 应付状态
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetPayableStatus()
        {
            return Enum.GetValues(typeof(ReceivableStatus))
                .Cast<ReceivableStatus>()
                .Select(x => new
                {
                    Value = (int)x,
                    Text = x.ToString()
                }).ToList();
        }
        /// <summary>
        /// 应付管理列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Pages<PayableShowDto>>> GetPayableList([FromQuery] PayableShowCommand command)
        {
            ApiResult<Pages<PayableShowDto>> result = new ApiResult<Pages<PayableShowDto>>();
            try
            {
                _logger.LogInformation("调用应付管理列表");
                result.Data = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用应付管理列表出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 应付信息反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Payable>> GetPayableModel([FromQuery] PayableModelCommand command)
        {
            ApiResult<Payable> result = new ApiResult<Payable>();
            try
            {
                _logger.LogInformation("调用应收管理反填");
                result.Data = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用应收管理反填出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
